package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.StudyPlan;
import com.battle.pojo.entity.StudyUser;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.studyUser.*;
import com.battle.pojo.vo.studyPlanUser.GetStudyPlanUserGraphVo;
import com.battle.pojo.vo.studyUser.*;
import com.battle.pojo.dto.studyUser.*;
import com.battle.service.repository.in.StudyPlanRepository;
import com.battle.service.repository.in.StudyUserRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.StudyUserService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：用户学习计划服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class StudyUserServiceImpl implements StudyUserService {

    private final StudyUserRepository studyUserRepository;
    private final StudyPlanRepository studyPlanRepository;
    private final UserRepository userRepository;


    public StudyUserServiceImpl(StudyUserRepository studyUserRepository, StudyPlanRepository studyPlanRepository, UserRepository userRepository) {
        this.studyUserRepository = studyUserRepository;
        this.studyPlanRepository = studyPlanRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建用户学习计划
     *
     * @param param
     */
    @Override
    public void createStudyUser(CreateStudyUserParam param) {
        StudyUser studyUser = new StudyUser();
        BeanUtils.copyProperties(param, studyUser);
        Long id = IdGenerator.SNOW.generate();
        studyUser.setId(id);
        studyUserRepository.create(studyUser);
    }

    /**
     * 修改用户学习计划
     *
     * @param param
     */
    @Override
    public void editStudyUser(EditStudyUserParam param) {
        Long id = param.getId();
        StudyUser studyUser = studyUserRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, studyUser);
        studyUserRepository.edit(studyUser);
    }

    /**
     * 移除用户学习计划
     *
     * @param id
     */
    @Override
    public void removeStudyUser(Long id) {
        studyUserRepository.removeById(id);
    }

    /**
     * 启动/禁用用户学习计划
     *
     * @param id
     */
    @Override
    public void enabledStudyUser(Long id) {
        StudyUser studyUser = studyUserRepository.getByIdCheck(id);
        studyUser.setIsDisable(!studyUser.getIsDisable());
        studyUserRepository.edit(studyUser);
    }

    /**
     * 获取用户学习计划详情
     *
     * @param id
     * @return
     */
    @Override
    public GetStudyUserVo getStudyUser(Long id) {
        StudyUser studyUser = studyUserRepository.getByIdCheck(id);
        GetStudyUserVo vo = new GetStudyUserVo();
        BeanUtils.copyProperties(studyUser, vo);
        return vo;
    }

    /**
     * 获取用户学习计划 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetStudyUserListVo> getStudyUserList(GetStudyUserListParam param) {

        SelectStudyUserDto dto = new SelectStudyUserDto();
        BeanUtils.copyProperties(param, dto);
        Page<GetStudyUserListVo> page = studyUserRepository.selectStudyUser(dto);
        List<GetStudyUserListVo> studyUserList = page.getRecords();
        if (CollectionUtil.isNotEmpty(studyUserList)){
            for (GetStudyUserListVo vo : studyUserList) {
                Float number=(vo.getCompleteNumber().floatValue()/ vo.getLearningNumber().floatValue());
                vo.setComplete(BigDecimal.valueOf(number*100));
            }
        }
        return new PageResult<>(studyUserList, page);

    }

    /**
     * 获取用户学习计划下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetStudyUserDownListVo> getStudyUserDownList(GetStudyUserDownListParam param) {
        StudyUser po = new StudyUser();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<StudyUser> studyUserList = studyUserRepository.getListByEntity(po);

        List<GetStudyUserDownListVo> list = new ArrayList<>();
        for (StudyUser studyUser : studyUserList) {
            GetStudyUserDownListVo vo = new GetStudyUserDownListVo();
            BeanUtils.copyProperties(studyUser, vo);
            list.add(vo);
        }
        return list;
    }

    /**
     * 根据学习计划id查看学习进度
     * @param studyPlanId
     * @return
     */
    @Override
    public List<GetStudyPlanUserGraphVo> getStudyPlanUserGraph(Long studyPlanId) {
        List<GetStudyPlanUserGraphVo> voList=new ArrayList<>();

        User userDetails=new User();
        List<User> listByEntity = userRepository.getListByEntity(userDetails);

        Integer userNumber=listByEntity.size();

        StudyPlan plan = studyPlanRepository.getByIdCheck(studyPlanId);
        //应该学习的计划数量
        Integer planNumber = plan.getLearningNumber();

        //根据计划查找用户学习
        StudyUser user=new StudyUser();
        user.setPlanId(studyPlanId);
        user.setIsDelete(false);
        user.setIsDisable(false);

        List<StudyUser> list = studyUserRepository.getListByEntity(user);
        //提取出学习数量
        if (CollectionUtil.isNotEmpty(list)){
            //根据数量进行分组
            List<Integer> learningNumberList = list.stream().map(StudyUser::getCompleteNumber).distinct().collect(Collectors.toList());
            Map<Integer, List<StudyUser>> leanNumberMap = list.stream().collect(Collectors.groupingBy(StudyUser::getCompleteNumber));

            for (Integer learningNumber : learningNumberList) {
                GetStudyPlanUserGraphVo vo = new GetStudyPlanUserGraphVo();
                vo.setLearningNumber(learningNumber);
                //已学习的量/应该学习的量
                BigDecimal divide = BigDecimal.valueOf(learningNumber).divide(BigDecimal.valueOf(planNumber), 2, BigDecimal.ROUND_HALF_UP);
                //*100
                divide=divide.multiply(BigDecimal.valueOf(100));
                vo.setStudyProgress(divide);
                List<StudyUser> studyUsers = leanNumberMap.get(learningNumber);
                if (CollectionUtil.isNotEmpty(studyUsers)){
                    vo.setUserCount(studyUsers.size());
                    userNumber=userNumber-studyUsers.size();
                }
                voList.add(vo);
            }
        }
        GetStudyPlanUserGraphVo vo = new GetStudyPlanUserGraphVo();
        vo.setLearningNumber(0);
        //已学习的量/应该学习的量
        vo.setStudyProgress(BigDecimal.ZERO);
        vo.setUserCount(userNumber);
        voList.add(vo);
        return voList;
    }


}



